import 'package:calendar_date_picker2/calendar_date_picker2.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class DatePickerWidgetWrapper extends StatelessWidget {
  DatePickerWidgetWrapper(
      {super.key,
      required this.calendarType,
      required this.defaultValue,
      this.onConfirm,
      this.onCancel});

  // 单选日期选择器构造函数
  DatePickerWidgetWrapper.single(
      {super.key, required this.defaultValue, this.onConfirm, this.onCancel})
      : calendarType = CalendarDatePicker2Type.single;

  // 范围日期选择器构造函数
  DatePickerWidgetWrapper.range(
      {super.key, required this.defaultValue, this.onConfirm, this.onCancel})
      : calendarType = CalendarDatePicker2Type.range;

  // 多选日期选择器构造函数
  DatePickerWidgetWrapper.multi(
      {super.key, required this.defaultValue, this.onConfirm, this.onCancel})
      : calendarType = CalendarDatePicker2Type.multi;

  // 日历类型
  final CalendarDatePicker2Type calendarType;

  // 默认值
  final List<DateTime> defaultValue;

  // 确定按钮回调
  final void Function(List<DateTime>)? onConfirm;

  // 取消按钮回调
  final void Function()? onCancel;

  // 日历配置
  CalendarDatePicker2Config get config =>
      CalendarDatePicker2Config(calendarType: calendarType);

  // 选中的日期（日期变化时存储，确认回调触发时返回）
  List<DateTime> _selectedDates = [];
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            TextButton(
                onPressed: () {
                  if (onCancel != null) {
                    onCancel!();
                  }
                  Navigator.pop(context);
                },
                child: const Text("取消")),
            TextButton(
                onPressed: () {
                  if (onConfirm != null) {
                    onConfirm!(_selectedDates);
                  }
                  Navigator.pop(context);
                },
                child: const Text("确定"))
          ],
        ),
        CalendarDatePicker2(
          config: config,
          value: defaultValue,
          onValueChanged: (times) {
            _selectedDates = times;
          },
        )
      ],
    );
  }
}
